package com.yaozheng.vocationaltraining.activity;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.service.LoginService;
import com.yaozheng.vocationaltraining.service.impl.LoginServiceImpl;
import com.yaozheng.vocationaltraining.utils.ACache;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

/**
 * 登录界面
 *
 * @author Administrator
 */
@EActivity(R.layout.activity_login)
public class LoginActivity extends BaseActivity implements IBaseView {
    @Bean(LoginServiceImpl.class)
    LoginService loginService;
    @ViewById
    EditText loginEditName, loginEditPassword;

    ACache aCache;

    String userName;

    /**
     * 忘记密码点击
     */
    public void forgotPasswordClick(View view) {
        alertMessage(R.string.forgotPasswordAlertStr);
    }

    @AfterViews
    public void initView() {
        loginService.init(this);
        aCache = ACache.get(this);
    }

    /**
     * 提交
     *
     * @param view
     */
    public void submitClick(View view) {
        userName = loginEditName.getText().toString();
        String passWord = loginEditPassword.getText().toString();
        if (userName == null || "".equals(userName)) {
            alertMessage(R.string.inputNameErrorStr);
            return;
        }
        if (passWord == null || "".equals(passWord)) {
            alertMessage(R.string.inputPassWordErrorStr);
            return;
        }
        showProgressDialog("登录中...");
        loginService.doLogin(userName, passWord);
    }

    @UiThread
    @Override
    public void responseSuccess(JSONObject jsonObject) {
        cancelProgressDialog();
        setToken(TypeUtils.getJsonString(
                TypeUtils.getJsonObject(jsonObject, "data"), "token", null));
        aCache.remove("homeDataCache");
        aCache.put("userName", userName);
        alertMessage("登录成功!");
        enterHome();

    }

    /**
     * 进入主页
     */
    public void enterHome() {
        Intent intent = getIntent();
        intent.setClassName(this, Constants.HOME_ACTIVITY_CLASS_NAME);
        startActivity(intent);
        finish();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
    }

    @UiThread
    @Override
    public void responseError(String error) {
        cancelProgressDialog();
        if (error != null) {
            alertMessage(error);
        }

    }

    @Override
    public boolean isResponseResult() {
        return !isFinishing();
    }
}
